﻿using Abp.Domain.Repositories;
using Camc.Duct.Customer;
using Camc.Duct.ProduceTask.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Duct.ProduceTask
{
	public class LaborAppService:DuctAppServiceBase
	{
		private readonly IRepository<Order, long> _orderRepository;
		private readonly IRepository<Contact> _contactRepository;
		private readonly IRepository<Process, long> _processRepository;

		public LaborAppService(IRepository<Order, long> orderRepository, IRepository<Contact> contactRepository, IRepository<Process, long> processRepository)
		{
			_orderRepository = orderRepository;
			_contactRepository = contactRepository;
			_processRepository = processRepository;
		}

		public void TechnicianSign(long orderId)
		{
			var order = _orderRepository.Get(orderId);
			order.Technician = GetCurrentUser().FullName;
			_orderRepository.Update(order);
		}

		public void AuditorSign(long orderId)
		{
			var order = _orderRepository.Get(orderId);
			order.Auditor = GetCurrentUser().FullName;
			order.AuditTime = DateTime.Now;
			_orderRepository.Update(order);
		}


		public OrderForLaborOutput GetOrderInfo(long orderId)
		{
			var order = _orderRepository.GetAllIncluding(c=>c.Customer).Where(c=>c.Id == orderId && c.IsDeleted == false).FirstOrDefault();
			var output = ObjectMapper.Map<OrderForLaborOutput>(order);
			return output;
		}

		public ICollection< ProcessDto> GetProcessesByOrder(long orderId)
		{
			var processes = _processRepository.GetAll().Where(c=>c.OrderId == orderId);

			return ObjectMapper.Map<ICollection<ProcessDto>>(processes);
		}

		public void SaveProcesses(ICollection<ProcessDto> processDtos)
		{
			foreach (var processDto in processDtos)
			{
				var process = _processRepository.Get(processDto.Id);

				process.Maker = processDto.Maker;
				process.Name = processDto.Name;
				process.TimeOfManufacture = processDto.TimeOfManufacture;
				process.TimeOfPreparation = processDto.TimeOfPreparation;
				process.Amount = processDto.Amount;
				process.IsPivotal = processDto.IsPivotal;
				_processRepository.Update(process);
			}
		}
	}
}
